﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace IceBird
{
	/// <summary>
	/// 长度限制
	/// </summary>
	public class LengthLimit
	{
		public const int maxNameLength = 40;		//用户名长度限制
		public const int maxPwdLength = 40;			//密码长度限制
		public const int maxCountPerQuery = 100;	//每次获取的Group/Topic/Message数目的上限
		public const int maxMessageLength = 276;	//一条Message的长度限制（=12×23）
		public const int maxGroupLength = 40;		//Group名字的长度限制
		public const int maxTopicLength = 40;		//Topic名字的长度限制
	}

	/// <summary>
	/// 登陆状态
	/// </summary>
	public enum LoginState
	{
        LOGIN = 0,
        LOGOUT = 1,
        HIDDEN = 2,
        WRONGPASSWORD = -1,
        USERNOTEXIST = -2,
        DBERROR = -3,
	}

	/// <summary>
	/// 注册状态
	/// </summary>
	public enum AssignState
	{
		SUCCEED = 0,
		NAME_USED = 1,				
		NAME_EMPTY = 2,
		/// <summary>
		/// 允许的长度为LengthLimit.maxNameLength
		/// </summary>
		NAME_TOO_LONG = 3,
		/// <summary>
		/// 允许在[33,127]中的字符
		/// </summary>
		NAME_ERROR_CHAR = 4,
		/// <summary>
		/// //允许的长度为LengthLimit.maxPwdLength
		/// </summary>
		PWD_TOO_LONG = 5,
		/// <summary>
		/// 允许大于等于32的字符
		/// </summary>
		PWD_ERROR_CHAR = 6,		
		/// <summary>
		/// 数据库操作错误
		/// </summary>
		ERROR_WHILE_READ_DB = -1
	}

	/// <summary>
	/// 新建Group/Topic/Message成功与否的状态
	/// </summary>
	public enum CreateState
	{
		Pass = 0,
		/// <summary>
		/// 传入的ID在上级中不存在
		/// </summary>
		NO_SUCH_ID = 1,
		/// <summary>
		/// 传入的文本长度太长，超过了LengthLimit.maxGroupLength/maxTopicLength/maxMessageLength
		/// </summary>
		TOO_LONG = 2,
		/// <summary>
		/// 数据库操作错误
		/// </summary>
		ERROR_WHILE_READ_DB = -1
	}

	/// <summary>
	/// 获取Group/Topic/Message编号的排序方法
	/// </summary>
	public enum SortForm{
		/// <summary>
		/// 默认，前若干个，从1开始编号，区间为左闭右开。例如[4,7)表示4、5、6
		/// </summary>
		DEFAULT = 0,
		/// <summary>
		/// 后若干个，从1开始编号，区间为左闭右开。例如总共有10个，[4,7)表示5、6、7
		/// </summary>
		LAST = 1
	}

	/// <summary>
	/// 在获取Group/Topic/Message时可能遇到的错误
	/// </summary>
	public enum DBError
	{
		OK = 0,
		/// <summary>
		/// 数据库操作错误
		/// </summary>
		DB_OPTION_ERROR = 1,
		/// <summary>
		/// 下界大于上界
		/// </summary>
		LOWER_BOUND_LARGER_THAN_HIGHER_BOUND = -1,
		/// <summary>
		/// 申请的条目数超过了LengthLimit.maxCountPerQuery，此时会将上界强制缩小至最大值并正常返回一个数组
		/// </summary>
		SCOPE_TOO_LARGE = 2,
		/// <summary>
		/// 不存在传入的GroupID/TopicID
		/// </summary>
		NO_SUCH_ID = 3,
		UNKNOWN = 4
	}

	public class Group
	{
		public int id;
		public string name;
		public int topicCnt;
	}

	public class Topic
	{
		public int id;
		public int groupId;
		public string title;
		public DateTime time;
		public int userId;
		public int idInGroup;
		public int replyCnt;
	}

	public class Message
	{
		public int id;
		public int userId;
		public DateTime time;
		public int topicId;
		public string content;
		public int idInTopic;
	}

	public class MessageSet
	{
		public DBError err;
		public Topic topic;
		public List<Message> Messages;
		public MessageSet() { topic = new Topic(); Messages = new List<Message>(); }
	}

	public class TopicSet
	{
		public DBError err;
		public Group group;
		public List<Topic> Topics;
		public TopicSet() { group = new Group(); Topics = new List<Topic>(); }
	}

	public class GroupSet
	{
		public DBError err;
		public int groupCount;
		public List<Group> Groups;
		public GroupSet() { groupCount = 0;Groups = new List<Group>(); }
	}

	public class AssignResult
	{
		public AssignState assignState;
		public int userId;
	}

	public class CreateResult
	{
		public CreateState createState;
		public int id;
	}
}
